package com.yenlien.traceyun.webase.utils;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.webase.exception.BusinessException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.util.List;


/**
 * @author wangyang@3sqy.cn
 * @version V1.0
 * @date 2017年11月15日
 * @Description:检验参数
 */
public class CheckParams {

    /**
     * 检查BindingResul绑定数据t
     *
     * @param br
     * @throws BusinessException
     */
    public static void checkErrors(BindingResult br) throws BusinessException {
        if (br.hasErrors()) {
            List<ObjectError> errors = br.getAllErrors();
            ObjectError error = errors.get(0);
            throw new BusinessException(error.getDefaultMessage());
        }
    }

    /**
     * 检查params
     *
     * @param params
     * @param needs  必要字段
     * @throws BusinessException
     */
    public static void checkJson(JSONObject params, String[] needs) throws BusinessException {
        if (params == null) {
            throw new BusinessException("C100002", "json参数错误");
        }
        if (needs != null && needs.length > 0) {
            for (String need : needs) {
                if (StringUtils.isEmpty(params.getString(need))) {
                    throw new BusinessException("C100001", "必要参数[" + need + "]不完整");
                }
            }
        }
    }

    /**
     * 检查header
     *
     * @param request
     * @param needs
     */
    public static void checkHeader(HttpServletRequest request, String[] needs) {
        if (needs != null && needs.length > 0) {
            for (String need : needs) {
                String value = request.getHeader(need);
                if (StringUtils.isEmpty(value)) {
                    throw new BusinessException("C100001", "必要参数[" + need + "]不完整");
                }
            }
        }
    }


    /**
     * 检查对象参数
     *
     * @param obj
     * @param needs
     * @throws BusinessException
     */
    public static void checkBean(Object obj, String[] needs) throws BusinessException {
        if (needs != null && needs.length > 0) {
            try {
                for (String need : needs) {
                    Field field = null;
                    try {
                        field = obj.getClass().getDeclaredField(need);
                    } catch (Exception e) {
                        field = obj.getClass().getSuperclass().getDeclaredField(need);
                    }
                    field.setAccessible(true);
                    Object value = field.get(obj);
                    String typename = field.getType().getSimpleName();
                    if (typename.equals("String")) {
                        if (value == null || value.toString().equals("")) {
                            throw new BusinessException("C100001", "必要参数[" + need + "]不完整");
                        }
                    } else if (typename.equals("Integer") || typename.equals("Long")) {
                        if (value == null) {
                            throw new BusinessException("C100001", "必要参数[" + need + "]不完整");
                        }
                    } else if (typename.equals("long") || typename.equals("int")) {
                        if (value.toString().equals("0")) {
                            throw new BusinessException("C100001", "必要参数[" + need + "]不完整");
                        }
                    }

                }
            } catch (Exception e) {
                if (!(e instanceof BusinessException)) {
                    e.printStackTrace();
                    throw new BusinessException("C100003", "参数发生异常");
                } else {
                    throw (BusinessException) e;
                }
            }
        }
    }

    /**
     * 检查对象参数
     *
     * @param request
     * @param needs
     * @throws BusinessException
     */
    public static void checkRequest(HttpServletRequest request, String[] needs) throws BusinessException {
        if (needs != null && needs.length > 0) {
            for (String need : needs) {
                String value = request.getParameter(need);
                if (StringUtils.isEmpty(value)) {
                    throw new BusinessException("C100001", "必要参数[" + need + "]不完整");
                }
            }
        }
    }

}
